﻿using PhuLeBooze.Global;
using PhuLeBussinessObject.CProductType;
using PhuLeDao.Product;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Xml;

namespace PhuLeBooze.Administrator.Views.Product.Service
{
    /// <summary>
    /// Summary description for productimportexcelservice
    /// </summary>
    public class productimportexcelservice : IHttpHandler, IReadOnlySessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request["Action"];
            string output = string.Empty;
            switch (action)
            {
                //case "INSERT":
                //case "UPDATE":
                //case "DELETE":
                //    output = UpdateProject(context);
                //    break;
                default:
                    output = GetProjectList(context);
                    break;
            }
            context.Response.Write(output);
        }
        //protected string UpdateProject(HttpContext context)
        //{

        //    string inputValue = GetInputValue(context);
        //    /* Action */
        //    CProductTypeDao core = new CProductTypeDao();
        //    CApplicationMessage message = core.ActionProductType(inputValue);
        //    string output = new JavaScriptSerializer().Serialize(message);
        //    return output;
        //}
        protected string GetProjectList(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            string _search = request["_search"];
            string numberOfRows = request["rows"];
            string pageIndex = request["page"];
            string sortColumnName = request["sidx"];
            string sortOrderBy = request["sord"];
            CProductDao core = new CProductDao();

            CCore cCore = new CCore();
            string inputValue = cCore.GetInputValue(context);
            IList<CProduct> list = core.GetProductImportTemp(inputValue);
            int totalRecords = core.LastOutputValue.TotalRows;
            int totalPages = core.LastOutputValue.TotalPages;
            string output = BuildJQGridResults(list, Convert.ToInt32(numberOfRows), Convert.ToInt32(pageIndex), totalPages);
            return output;
        }
        protected string BuildJQGridResults(IList<CProduct> projects, int numberOfRows, int pageIndex, int totalPages)
        {
            if (projects != null && projects.Count > 0)
            {
                CGrid.JQGridResults result = new CGrid.JQGridResults();
                List<CGrid.JQGridRow> rows = new List<CGrid.JQGridRow>();
                foreach (CProduct project in projects)
                {
                    CGrid.JQGridRow row = new CGrid.JQGridRow();
                    row.id = project.ID;
                    row.cell = new string[20];
                    row.cell[0] = project.ID.ToString();
                    row.cell[1] = project.Code;
                    row.cell[2] = project.NameVN;
                    row.cell[3] = project.NameEN;
                    row.cell[4] = project.Origin;
                    row.cell[5] = project.DescriptionVN;
                    row.cell[6] = project.DescriptionEN;
                    row.cell[7] = project.Specification.ToString();
                    row.cell[8] = project.Price.ToString();
                    row.cell[9] = project.ZOrder.ToString();
                    row.cell[10] = string.Format("<img  class='imgProjectImage' src='{0}' FileName='{0}'/>", project.Logo);
                    row.cell[11] = string.Format("<img class='imgProjectImage' src='{0}' FileName='{0}'/>", project.Thumbnail);
                    row.cell[12] = string.Format("<img  class='imgProjectImage' src='{0}' FileName='{0}'/>", project.Image1);
                    row.cell[13] = string.Format("<img class='imgProjectImage' src='{0}' FileName='{0}'/>", project.Image2);
                    row.cell[14] = string.Format("<img  class='imgProjectImage' src='{0}' FileName='{0}'/>", project.Image3);
                    row.cell[15] = string.Format("<img class='imgProjectImage' src='{0}' FileName='{0}'/>", project.Image4);
                    row.cell[16] = project.LastModify.ToString("MM/dd/yyyy HH:mm");
                    row.cell[17] = project.Status.ToString();
                    row.cell[18] = project.Status.ToString();
                    row.cell[19] = string.Format("<img class='imgAction' src='../../Images/16x16/edit.png' onclick=\"ShowEdit('{0}')\"/>", project.ID);
                    rows.Add(row);
                }
                result.rows = rows.ToArray();
                result.page = pageIndex;
                result.total = totalPages;
                result.records = numberOfRows;
                return new JavaScriptSerializer().Serialize(result);
            }
            else
            {
                return string.Empty;
            }
        }

        public string GetInputValue(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("Root");
            XmlElement InputValue = doc.CreateElement("InputValue");
            root.AppendChild(InputValue);
            InputValue.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            InputValue.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN.ToString());

            StringBuilder qsXml = new StringBuilder();
            string[] allKeys = request.Form.AllKeys;
            if (allKeys.Length == 0)
            {
                allKeys = request.QueryString.AllKeys;
            }
            foreach (string pKey in allKeys)
            {
                string key = pKey.Trim();
                string value = request[key].Trim();

                if (key.IndexOf("Gift_") != -1)
                {
                    XmlElement gift = doc.CreateElement("Gift");
                    string giftID = key.Replace("Gift_", "");
                    gift.SetAttribute("GiftID", giftID);
                    gift.SetAttribute("Quantity", value);
                    root.AppendChild(gift);
                }
                else
                {
                    InputValue.SetAttribute(key, value);
                }
            }
            return root.InnerXml;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}